﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.SessionState;

namespace SaturniLibrary.Web.Log
{
    public class ThreadLogging
    {
        [ThreadStatic]
        private static ThreadLogging threadLogging;

        private StopWatch stopWatch;

        StringBuilder builder = new StringBuilder();

        StringBuilder transactionLogs = new StringBuilder();

        private string ticket;

        private Exception exception;

        private HttpSessionState session;

        public HttpSessionState Session
        {
            get { return session; }
            set { session = value; }
        }

        public Exception Exception
        {
            get { return exception; }
            set { exception = value; }
        }

        public string Ticket
        {
            get { return ticket; }
            set { ticket = value; }
        }

        public void LogTransaction(string message)
        {
            transactionLogs.AppendLine(message);
        }

        public void Log(string message)
        {
            builder.AppendLine(message);
        }

        public string GetAllLogs()
        {
            return builder.ToString();
        }

        public string GetAllTransactions()
        {
            return transactionLogs.ToString();
        }

        public void StartStopWatch()
        {
            stopWatch = new StopWatch();
        }

        public StopWatch GetStopWatch()
        {
            return stopWatch ?? new StopWatch();
        }

        public void Clear()
        {
            builder = new StringBuilder();
            transactionLogs = new StringBuilder();
            exception = null;
            ticket = string.Empty;
            session = null;
        }

        public static ThreadLogging GetLogger()
        {
            if (threadLogging == null)
            {
                threadLogging = new ThreadLogging();
            }
            return threadLogging;
        }
    }
}
